﻿using RantopadService.Business;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RantopadService.query
{
    public partial class QueryUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }

        private void BindData()
        {
            var data = UserBusiness.getAllUsers();
            GridView1.DataSource = data;
            GridView1.DataBind();
            LabelMsg.Text = string.Format("共{0}条数据", data.Rows.Count);
        }
        private void BindData(string wherelike)
        {
            var data = UserBusiness.getAllUsers(wherelike, onLine.SelectedValue);
            GridView1.DataSource = data;
            GridView1.DataBind();
            LabelMsg.Text = string.Format("共{0}条数据",data.Rows.Count);
           
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void Search_Click(object sender, EventArgs e)
        {
            BindData(TextWhereLike.Text.ToString());
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //GridView1.EditIndex = -1;
            //GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //GridView1.EditIndex = e.NewEditIndex;
            //GridView1.DataBind();
            
            //var name = GridView1.DataKeys[e.].Value.ToString();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var name =GridView1.DataKeys[e.RowIndex].Value.ToString();       

            if (UserBusiness.delUserByName(name) > 0)
                Response.Write("<script>alert('删除成功！')</script>");
            else
                Response.Write("<script>alert('删除失败！')</script>");
            BindData();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) 
            { 
            ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[0].Text + "\"吗?')"); 
            } 
            } 
        }

        protected void ButAddOrUpdate_Click(object sender, EventArgs e)
        {
            if(chechUser())
            {
                int success = 0;

                if (ButAddOrUpdate.Text == "添加") success = UserBusiness.addUser(UserEmail.Text, UserName.Text, MD5Helper.GetMd5Str( Psd.Text));

                if (ButAddOrUpdate.Text == "修改") success = UserBusiness.updateUser(UserEmail.Text, UserName.Text, MD5Helper.GetMd5Str(Psd.Text));

                if(success>0)
                {
                    BindData();
                    Msg.Text = ButAddOrUpdate.Text + "成功";
                }
                else
                {
                    Msg.Text = ButAddOrUpdate.Text + "失败";
                }

            }
        }

        private bool chechUser()
        {
            Msg.Text = "";
            if(UserEmail.Text == ""||UserName.Text == ""||Psd.Text == ""||rPsd.Text == "")
            {
                Msg.Text = "信息不全";
                return false;
            }
            if (Psd.Text != rPsd.Text)
            {
                Msg.Text = "两次密码不一样";
                return false;
            }
            return true;
        }

        protected void ButClear_Click(object sender, EventArgs e)
        {
            UserEmail.Text = "";
            UserName.Text = "";
            Psd.Text = "";
            rPsd.Text = "";
            ButAddOrUpdate.Text = "添加";
            UserName.Enabled = true;

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);

                // Get the last name of the selected author from the appropriate
                // cell in the GridView control.
                GridViewRow selectedRow = GridView1.Rows[index];
            if (e.CommandName == "EditUser")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                
                //TableCell contactName = selectedRow.Cells[1];
                //string contact = contactName.Text;
                //Msg.Text = contact;

                UserEmail.Text = selectedRow.Cells[1].Text;
                UserName.Text = selectedRow.Cells[0].Text;

                ButAddOrUpdate.Text = "修改";
                UserName.Enabled = false;
            }
            else if(e.CommandName=="SendMessage")
            {
                SendMessage(selectedRow);
            }
        }

        private void SendMessage(GridViewRow selectedRow)
        {
            Response.Redirect("../PuMessage/QueryUserMessage.aspx?userid=" + selectedRow.Cells[0].Text);
            //MessageBusiness.addMessageToUser(selectedRow.Cells[0].Text,)
            //throw new NotImplementedException();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
        }
        protected void OutToExcel_Click(object sender, EventArgs e)
        {
            Helper.ExportToExcel(this, (DataTable)GridView1.DataSource, "Users");
        } 
    }
}